package com.ebelter.btcomlib.models.bluetooth.products.temperature;



import com.ebelter.btcomlib.models.bluetooth.bean.SendMessage;
import com.ebelter.btcomlib.utils.log.LogUtils;

import java.util.Calendar;

/**
 * byte制包工具类
 */
public class TemperatureBytesMakeFatory {

    private static final String TAG = "TemperatureBytesMakeFatory";

    //心跳包字节
   private static final byte[] HEARTCHECKBYTES = {(byte) 0xab, 0x01, (byte) 0xb0};


    private static final String HEARTCHECK="心跳包";
    public static SendMessage getHEARTSmg(){
        SendMessage heartMsg=new SendMessage();
        heartMsg.datas=HEARTCHECKBYTES;
        heartMsg.desc=HEARTCHECK;
        return heartMsg;
    }


    private static final String CELAR_ALL_HISTORY="擦除全部历史数据";
    private static final byte[] CELAR_ALL_HISTORY_BYTES=new byte[]{(byte) 0xe0,0,0,0,0,0,0,0,0, (byte) 0xe0};
    public static SendMessage getCELAR_ALL_HISTORY(){
        SendMessage heartMsg=new SendMessage();
        heartMsg.datas=CELAR_ALL_HISTORY_BYTES;
        heartMsg.desc=CELAR_ALL_HISTORY;
        return heartMsg;
    }





//    寻找设备
//    发送指令F8:46:49:4E:44:66:69:6E:64:BA 设备蓝灯快速闪灯10s


    private static final String FindDevices="寻找设备 设备蓝灯快速闪灯10s";
    private static final byte[] FindDevices_BYTES=new byte[]{(byte) 0xf8,0x46,0x49,0x4e,0x44,0x66,0x69,0x6e,0x64, (byte) 0xba};
    public static SendMessage findDevices(){
        SendMessage heartMsg=new SendMessage();
        heartMsg.datas=FindDevices_BYTES;
        heartMsg.desc=FindDevices;
        return heartMsg;
    }




    //同步系统时间
    private byte[] getSystemClock(){
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        int date = cal.get(Calendar.DATE)-1;
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);


        int yearLowHex = year & 0xff;
        int yearHeightHex = (year >> 8) & 0xff;
        LogUtils.i(TAG,"year="+year+"--yearLowHex="+yearLowHex+"--yearHeightHex="+yearHeightHex+"--month="+month
        +"--date="+date+"--hour="+hour+"--minute="+minute+"--second="+second+"--week=");
        byte[] bytes = {
                (byte) 0xb0,
                (byte) yearHeightHex,
                (byte) yearLowHex,
                (byte) month,
                (byte) date,
                (byte) hour,
                (byte) minute,
                (byte) second,
                0,
                0,

        };

//        时间是2017-6-16 9:37，设置时钟的指令 正确的示例
//        B0 07 E1 05 0F 09 25 22 00 FC

       // LogUtils.i(TAG,"加校验码之前--"+ BytesUtils.bytes2HexStr(bytes));
        addCheckCode(bytes);
        //LogUtils.i(TAG,"加校验码之后--"+ BytesUtils.bytes2HexStr(bytes));

        return bytes;
    }

    private byte[] addCheckCode(byte[] bytes){
        int re=0;
        for (int i=0;i<9;i++){
            re=re+bytes[i];
        }
        bytes[9]= (byte) re;
        return bytes;
    }


    private static final String SystemClock="下发系统时间";
    public SendMessage getSyncSystemClock() {
        SendMessage sendUserMsg=new SendMessage();
        sendUserMsg.datas=getSystemClock();
        sendUserMsg.desc=SystemClock;
        return sendUserMsg;
    }

//    联上的时候，app请求历史数据，发送下面指令
//    C0:01:00:00:00:00:00:00:00:C1

    private static final byte[] getHistoryData_BYTES=new byte[]{(byte) 0xc0,0x01,0,0,0,0,0,0,0, (byte) 0xc1};
    private static final String getHistoryData="请求历史数据";
    public SendMessage getHistory() {
        SendMessage sendUserMsg=new SendMessage();
        sendUserMsg.datas=getHistoryData_BYTES;
        sendUserMsg.desc=getHistoryData;
        return sendUserMsg;
    }



}
